package com.csci4448.distributedcomputing;

//import java.util.Calendar;

import com.csci4448.distributedcomputing.R;
import android.widget.TextView;

import android.app.Activity;
/*import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;*/
import android.content.Intent;
import android.os.Bundle;
/*import android.text.format.DateFormat;
import android.text.format.Time;
import android.util.Log;*/
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/*import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;*/

public class Main extends Activity implements OnClickListener {
	
    /*private static final int ALERT_DIALOG_ID = 0;
    private static final int DATE_DIALOG_ID  = 1;
    private static final int TIME_DIALOG_ID  = 2;*/
    
   /* private Time selectedDay  = null;
    private int  mHour        = -1;
    private int  mMinute      = -1;*/
	
	private Boolean running = false;
    String server_url = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /*Button alert_button = (Button) findViewById(R.id.alert_button);
        Button date_button =  (Button) findViewById(R.id.date_button);
        Button time_button =  (Button) findViewById(R.id.time_button);*/
        
        Button chooseProject = (Button) findViewById(R.id.choose_proj_button);
        Button startProject = (Button) findViewById(R.id.start_computation_button);
        
    /*    alert_button.setOnClickListener(this);
        date_button.setOnClickListener(this);
        time_button.setOnClickListener(this);*/
        chooseProject.setOnClickListener(this);
        startProject.setOnClickListener(this);
        
    }
    
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        server_url = data.getStringExtra("SERVER_URL");
    }

    public void onClick(View v) {
    	
    	switch (v.getId()) {
    	    /*case R.id.alert_button:
    	    	showDialog(ALERT_DIALOG_ID);
        		break;
    	    case R.id.date_button:
    	    	showDialog(DATE_DIALOG_ID);
    		    break;
    	    case R.id.time_button:
    	    	showDialog(TIME_DIALOG_ID);
    		    break;*/
    		case R.id.choose_proj_button:
    			Intent chooseProj = new Intent(Main.this, ProjectChooser.class);
    			startActivityForResult(chooseProj, 1);
    		break;
    		case R.id.start_computation_button:
    			if(running) {
    				((Button) findViewById(R.id.start_computation_button)).setText(R.string.start_computation);
    				((TextView) findViewById(R.id.computation_status)).setText(R.string.computation_stopped);
    				((TextView) findViewById(R.id.computation_status)).setTextColor(R.color.project_stopped);
    				running = false;

    			}
    			else {
    				((Button) findViewById(R.id.start_computation_button)).setText(R.string.stop_computation);
    				((TextView) findViewById(R.id.computation_status)).setText(R.string.computation_running);
    				((TextView) findViewById(R.id.computation_status)).setTextColor(R.color.project_running);
                    

                    Intent i = new Intent(getApplicationContext(), WorkerService.class);
                    i.putExtra("SERVER_URL", server_url);

                    startService(i);
    				running = true;
    			}
    			//((TextView)findViewById(R.id.current_project_description)).setText("hello world");
    	}
    }

    /*protected Dialog onCreateDialog(int id) {
    	switch (id) {
    	    case ALERT_DIALOG_ID:
    	        Log.i("Computation", "Creating AlertDialog");
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Do you want to cancel the download?").setTitle("Cancel Download?");
                builder.setNegativeButton("No!",
                    new DialogInterface.OnClickListener() {
     					@Override
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	Main.this.dismissDialog(ALERT_DIALOG_ID);
                        	TextView alert_result = (TextView) findViewById(R.id.alert_response);
                        	alert_result.setText("NO!");
                        }
                    });
                builder.setPositiveButton("YES!",
                    new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int whichButton) {
                        	Main.this.dismissDialog(ALERT_DIALOG_ID);
                        	TextView alert_result = (TextView) findViewById(R.id.alert_response);
                        	alert_result.setText("YES!");
						}                	
                    });
    	        return builder.create();
    	    case DATE_DIALOG_ID:
    	        Log.i("FunWithDialogs", "Creating DatePicker");
    	        return
    	        	new DatePickerDialog(this,
    	        		new DatePickerDialog.OnDateSetListener() {
    	        			@Override
    	        			public void onDateSet(DatePicker picker, int year, int month, int day) {
                            	Main.this.dismissDialog(DATE_DIALOG_ID);
    	        				selectedDay = new Time();
    	        				selectedDay.set(day, month, year);
    	        				TextView date_response = (TextView) findViewById(R.id.date_response);
    	        				date_response.setText(DateFormat.format("MMMM dd, yyyy", selectedDay.toMillis(true)));
    	        			}
    	        		}, 2011, 1, 1);
    	    case TIME_DIALOG_ID:
    	        Log.i("FunWithDialogs", "Creating TimePicker");
    	        return
    	            new TimePickerDialog(this,
    	            	new TimePickerDialog.OnTimeSetListener() {
							
							@Override
							public void onTimeSet(TimePicker picker, int hourOfDay, int minute) {
                            	Main.this.dismissDialog(TIME_DIALOG_ID);
								mHour = hourOfDay;
								mMinute = minute;
								TextView time_response = (TextView) findViewById(R.id.time_response);
								if (mHour > 12) {
									time_response.setText("" + (mHour - 12) + ":" + mMinute + " PM");
								} else if (mHour == 12) {
									time_response.setText("" + mHour + ":" + mMinute + " PM");
								} else if (mHour == 0) {
									time_response.setText("" + (mHour + 12) + ":" + mMinute + " AM");									
								} else {
									time_response.setText("" + mHour + ":" + mMinute + " AM");									
								}
							}
						}, 1, 1, false);
    	    default:
    	    	return null;
    	}
    }*/

   /* @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
    	switch (id) {
    	    case ALERT_DIALOG_ID:
    	    	break;
    	    case DATE_DIALOG_ID:
    	        Log.i("FunWithDialogs", "Preparing DatePicker");
    	        DatePickerDialog datePicker = (DatePickerDialog)dialog;
    	        if (selectedDay != null) {
    	        	datePicker.updateDate(selectedDay.year, selectedDay.month, selectedDay.monthDay);
    	        } else {
    	        	Calendar cal = Calendar.getInstance();
    	        	datePicker.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
    	        }
    	        break;
    	    case TIME_DIALOG_ID:
    	        Log.i("FunWithDialogs", "Preparing TimePicker");
    	        TimePickerDialog timePicker = (TimePickerDialog)dialog;
    	        if (mHour != -1) {
    	        	timePicker.updateTime(mHour, mMinute);
    	        } else {
    	        	Calendar cal = Calendar.getInstance();
    	        	timePicker.updateTime(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
    	        }
    	        break;
    	    default:
    	    	break;
    	}
    }*/

    
}
